package sk.openit.xml.jaxp.dom;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

import sk.openit.xml.entity.DbConfig;
import sk.openit.xml.entity.DbConfigTags;

public class DbConfigWriter implements DbConfigTags {
	private String fileName;
	
	public DbConfigWriter(String fileName) {
		this.fileName = fileName;
	}
	
	public void serialize(DbConfig dbConfig) {
		FileOutputStream fileOutputStream = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder loader = factory.newDocumentBuilder();
			Document document = loader.newDocument();

			Element config = document.createElement( CONFIG );
			Element host = document.createElement( HOST );
			Element port = document.createElement( PORT );
			Element instance = document.createElement( INSTANCE );
			Element database = document.createElement( DATABASE );
			Element user = document.createElement( USER );
			Element password = document.createElement( PASSWORD );
			document.appendChild(config);
			config.appendChild( host);
			config.appendChild( port );
			config.appendChild( instance );
			config.appendChild(database);
			config.appendChild(user);
			config.appendChild(password);

			config.setAttribute(DB_TYPE, dbConfig.getDbType());
			host.setTextContent(dbConfig.getHost());
			port.setTextContent(dbConfig.getPort());
			instance.setTextContent(dbConfig.getInstance());
			database.setTextContent(dbConfig.getDatabase());
			user.setTextContent(dbConfig.getUser());
			password.setTextContent(dbConfig.getPassword());

			
			DOMImplementationLS domImplementationLS = null;			
			//testing the support for DOM Load and Save
	        if(document.getFeature("Core","3.0")!=null && document.getFeature("LS","3.0")!=null) {       
	        	domImplementationLS = (DOMImplementationLS) document.getImplementation().getFeature("LS", "3.0");
	        	System.out.println("[Using DOM Load and Save]");
	        } else {
				System.out.println("[DOM Load and Save unsupported]");            
				System.exit(0);
			}
	        
	        fileOutputStream = new FileOutputStream(new File(fileName));
	        LSOutput lsOutput = domImplementationLS.createLSOutput();
	        lsOutput.setByteStream(fileOutputStream);
	        
	        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
	        lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
	        boolean status = lsSerializer.write(document, lsOutput);
	        
	        if (status) {
	        	System.out.println("[Serialization done!]");
	        } else {
	        	System.out.println("[Serialization failed!]");
	        }	        
		} catch (Exception ex) {
			ex.printStackTrace();
		}		
	}
}
